<template>
  <el-col
    :xs="24"
    :sm="24"
    :md="24"
    :lg="24"
    :xl="24"
    class="mb-[18px]"
    v-motion
    :initial="{
      opacity: 0,
      y: 100
    }"
    :enter="{
      opacity: 1,
      y: 0,
      transition: {
        delay: 400
      }
    }"
  >
    <el-card shadow="never">
      <div class="product-list">
        <div v-for="product in products" :key="product.id" class="product-item">
          <img :src="product.image" :alt="product.name" />
          <h3>{{ product.name }}</h3>
          <p>{{ product.description }}</p>
          <el-button
            @click="
              (formDialogVisible = true), (formData.product_name = product.name)
            "
          >
            下单
          </el-button>
        </div>
        <!-- <dialogForm v-model:visible="formDialogVisible" :data="formData" /> -->
      </div>
    </el-card>
  </el-col>
  <el-col
    :xs="24"
    :sm="24"
    :md="24"
    :lg="24"
    :xl="24"
    class="mb-[18px]"
    v-motion
    :initial="{
      opacity: 0,
      y: 100
    }"
    :enter="{
      opacity: 1,
      y: 0,
      transition: {
        delay: 400
      }
    }"
  >
    <el-card shadow="never">
      <!-- <template #header>
        <TypeIt
          :className="'type-it4'"
          :values="['111111']"
          :cursor="false"
          :speed="120"
        />
      </template> -->
      <!-- <el-skeleton animated :rows="7" :loading="loading">
        <template #default>
          <Line />
        </template>
      </el-skeleton> -->
      <!-- <pure-table :data="tableData" :columns="columns" /> -->
      <PureTableBar title="订单详情" @refresh="onSearch">
        <template #buttons>
          <!-- <el-button type="primary" :icon="useRenderIcon(AddFill)">
            新增用户
          </el-button> -->
        </template>
        <template v-slot="{ size, checkList }">
          <pure-table
            border
            align-whole="center"
            table-layout="auto"
            :loading="loading"
            :size="size"
            :data="dataList"
            :columns="columns"
            :checkList="checkList"
            :pagination="pagination"
            :paginationSmall="size === 'small' ? true : false"
            :header-cell-style="{
              background: 'var(--el-table-row-hover-bg-color)',
              color: 'var(--el-text-color-primary)'
            }"
            @selection-change="handleSelectionChange"
            @page-size-change="handleSizeChange"
            @page-current-change="handleCurrentChange"
          >
            <template #operation="{ row }">
              <el-button
                class="reset-margin"
                link
                type="primary"
                :size="size"
                @click="handleUpdate(row)"
                :icon="useRenderIcon(EditPen)"
              >
                修改
              </el-button>
              <el-popconfirm title="是否确认删除?">
                <template #reference>
                  <el-button
                    class="reset-margin"
                    link
                    type="primary"
                    :size="size"
                    :icon="useRenderIcon(Delete)"
                    @click="handleDelete(row)"
                  >
                    删除
                  </el-button>
                </template>
              </el-popconfirm>
              <el-dropdown>
                <el-button
                  class="ml-3 mt-[2px]"
                  link
                  type="primary"
                  :size="size"
                  @click="handleUpdate(row)"
                  :icon="useRenderIcon(More)"
                />
                <template #dropdown>
                  <el-dropdown-menu>
                    <el-dropdown-item>
                      <el-button
                        :class="buttonClass"
                        link
                        type="primary"
                        :size="size"
                        :icon="useRenderIcon(Password)"
                      >
                        查看详情
                      </el-button>
                    </el-dropdown-item>
                    <el-dropdown-item>
                      <el-button
                        :class="buttonClass"
                        link
                        type="primary"
                        :size="size"
                        :icon="useRenderIcon(Role)"
                      >
                        修改备注
                      </el-button>
                    </el-dropdown-item>
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </template>
          </pure-table>
        </template>
      </PureTableBar>
    </el-card>
  </el-col>
  <dialogForm v-model:visible="formDialogVisible" :data="formData" />
</template>

<script setup lang="ts">
defineOptions({
  name: "GetOrder"
});
import { useUser } from "./hook";
import { PureTableBar } from "@/components/RePureTableBar";
import { reactive, ref } from "vue";
import dialogForm from "./dialog.vue";
// import { getOrder_name } from "@/api/orders";
import { onMounted } from "vue";
import axios from "axios";
// import { tableData } from "../pure-table/high/data";
import { CodeToText } from "@/utils/chinaArea";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import Role from "@iconify-icons/ri/admin-line";
import Password from "@iconify-icons/ri/lock-password-line";
import More from "@iconify-icons/ep/more-filled";
import Delete from "@iconify-icons/ep/delete";
import EditPen from "@iconify-icons/ep/edit-pen";
// import Search from "@iconify-icons/ep/search";
// import Refresh from "@iconify-icons/ep/refresh";
// import AddFill from "@iconify-icons/ri/add-circle-line";

const {
  loading,
  columns,
  dataList,
  pagination,
  buttonClass,
  onSearch,
  handleUpdate,
  handleDelete,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange
} = useUser();

const formData = reactive({
  product_name: "",
  name: "",
  company: "",
  address: [],
  phone: "",
  order: "normal",
  date: "",
  remark: ""
});
const formDialogVisible = ref(false);
const products = reactive([
  {
    id: 1,
    name: "Product 1",
    description: "Description of product 1",
    image: "https://via.placeholder.com/150"
  },
  {
    id: 2,
    name: "Product 2",
    description: "Description of product 2",
    image: "https://via.placeholder.com/150"
  },
  {
    id: 3,
    name: "Product 3",
    description: "Description of product 3",
    image: "https://via.placeholder.com/150"
  },
  {
    id: 4,
    name: "Product 4",
    description: "Description of product 4",
    image: "https://via.placeholder.com/150"
  }
]);
// const columns: TableColumnList = [
//   {
//     label: "订单类型",
//     prop: "order"
//   },
//   {
//     label: "姓名",
//     prop: "name"
//   },
//   {
//     label: "单位",
//     prop: "company"
//   },
//   {
//     label: "地址",
//     prop: "address"
//   },
//   {
//     label: "下单时间",
//     prop: "created_at"
//   },
//   {
//     label: "产品名称",
//     prop: "product_name"
//   }
// ];
const tableData = ref();
interface UserInfo {
  username: string;
  roles: string[];
  expires: Date;
  refreshToken: string;
}
const username = ref<string>("");
onMounted(async () => {
  const userInfo = sessionStorage.getItem("user-info");
  if (userInfo) {
    const { username: u } = JSON.parse(userInfo) as UserInfo;
    if (u) {
      username.value = u;
    }
  }
  const response = await axios.get("/api/orders/", {
    params: {
      name: username.value
    }
  });
  tableData.value = response.data.map(item => {
    return {
      order: item.order,
      name: item.name,
      company: item.company,
      address:
        CodeToText[item.province_id] +
        CodeToText[item.city_id] +
        CodeToText[item.district_id],
      created_at: item.created_at,
      product_name: item.product_name
    };
  }, console.log("tableData", tableData.value));
});
</script>

<style>
.product-list {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
}

.product-item {
  margin: 1rem;
  text-align: center;
}

.product-item img {
  width: 150px;
  height: 150px;
  object-fit: cover;
  border-radius: 50%;
  margin-bottom: 1rem;
}

.product-item button {
  background-color: #00c4b1;
  color: #fff;
  border: none;
  padding: 0.5rem 1rem;
  border-radius: 4px;
  cursor: pointer;
}

:deep(.el-dropdown-menu__item i) {
  margin: 0;
}
</style>
